package com.剑指offer.第一章;

/**
 * 11 10  ==== 101
 */
public class 二进制加法 {

    public static String addBinary(String a, String b) {
        StringBuilder builder = new StringBuilder();
        int i = a.length() - 1;
        int j = b.length() - 1;
        int carry = 0;
        while (i >= 0 || j >= 0) {
            int bigA = i < 0 ? 0 : a.charAt(i--) - '0';
            int bigB = j < 0 ? 0 : b.charAt(j--) - '0';
            int sum = bigA + bigB + carry;
            carry = sum >= 2 ? 1 : 0;
            sum = sum >= 2 ? sum - 2 : sum;
            builder.append(sum);
        }
        if (carry == 1) {
            builder.append(carry);
        }
        return builder.reverse().toString();
    }

    public static void main(String[] args) {
        String s = addBinary("11", "10");
        System.out.println(s);
    }

}
